import { _decorator, Component, Node } from 'cc';
import { BasePanel } from './BasePanel';
import { _item, _platform, _player, _tip } from '../Main';
import { TT } from '../platforms/minis/TT';
import { BaseItem } from '../items/BaseItem';
const { ccclass, property } = _decorator;

@ccclass('ShortcutPanel')
export class ShortcutPanel extends BasePanel {
    
    panelName: string = '添加桌面'

    @property(Node)
    addNode: Node = null!

    @property(Node)
    gotNode: Node = null!

    start(): void {
        this.refreshButtons()
        super.start()
    }

    refreshButtons() {
        let got: boolean = _player.bonuses.shortcut <= 0
        this.addNode.active = !got
        this.gotNode.active = got
    }

    add() {
        if (_player.bonuses.shortcut <= 0) {
            this.close()
            return    
        }
        let tt = _platform.platform
        if (tt instanceof TT) {
            tt.addShortcut((success) => {
                this.close(() => {
                    if (success) {
                        _player.changeNumData('shortcut', _player.bonuses, -1, 'add', true, () => {
                            let item: BaseItem = _item.items.Coin
                            let num: number = 30
                            item.get(num, () => {
                                _tip.tip('获得' + num + '金币')
                            })
                            let pf = _platform.platform
                            if (pf instanceof TT) {
                                pf.reportAnalytics('Behavior', {
                                    purpose: '获取',
                                    type: '道具',
                                    name: item.showName,
                                    amount: num,
                                    source: this.panelName,
                                })
                            }
                        })
                    } else {
    
                    }
                })
            })
        } else {
            this.close()
        } 
    }

    got() {
        this.add()
    }
}


